use crate::handler::template_handler::template_handler;
use crate::handler::no_found_handler::no_found_handler;
use crate::http::http_request::{HttpMethod, HttpRequest};
use crate::http::http_response::{HttpResponse};
use std::path::Path;
use regex::Regex;
use tracing::info;
use crate::handler::message_handler::{message_handler};
use crate::handler::oauth2_handler::oauth2_handler;
use crate::handler::static_handler::static_handler;

// 定义路由函数
pub async fn route(request: &HttpRequest, response: &mut HttpResponse) {

    info!("route called with {:?}", request.uri);

    let re_index = Regex::new(r"^/(index/?)?$").unwrap();
    let re_static = Regex::new(r"^/static/.*$").unwrap(); // 修改正则表达式

    let re_post_message = Regex::new(r"^/message/?$").unwrap();

    let re_oauth2_callback = Regex::new(r"^/oauth2/callback/?$").unwrap();



    match request.uri.as_str() {
        uri if re_post_message.is_match(uri) => {
            match request.method {
                HttpMethod::POST => {
                    info!("Index handler for POST");
                    message_handler(request, response);
                },
                _ => {
                    info!("Index handler for other method");
                    HttpResponse::bad_request(response);
                }
            }
        },
        uri if re_index.is_match(uri) => {
            match request.method {
                HttpMethod::GET => {
                    info!("Index handler for GET");
                    template_handler(request, response);
                },
                _ => HttpResponse::bad_request(response)
            }

        },
        uri if re_static.is_match(uri) => {
            match request.method {
                HttpMethod::GET => {
                    info!("Index handler for GET");
                    static_handler(request, response);
                },
                _ => HttpResponse::bad_request(response),
            }

        },
        uri if re_oauth2_callback.is_match(uri) => {
            match request.method {
                HttpMethod::GET => {
                    info!("Index handler for GET");
                    oauth2_handler(request, response).await.expect("登录失败");
                },
                _ => HttpResponse::bad_request(response),
            }

        },
        _ => HttpResponse::not_found(response),
    }

}
